package com.peter.handler.ui.main;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.HandlerThread;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleObserver;
import androidx.lifecycle.OnLifecycleEvent;
import androidx.lifecycle.ViewModelProvider;

import com.peter.handler.R;

import org.jetbrains.annotations.NotNull;

import java.io.IOException;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.ResponseBody;

public class MainFragment extends Fragment implements View.OnClickListener {


    private static String Tag = "MainFragment";

    private MainViewModel mViewModel;

    private LifeCycleListener lifeCycleListener;


    public static MainFragment newInstance() {
        return new MainFragment();
    }


    @Override
    public void onCreate(@Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.e("===MyLocationListener", "onCreate");
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.main_fragment, container, false);

        button = v.findViewById(R.id.button);
        textView = v.findViewById(R.id.message);
        Log.e("===MyLocationListener", "OnCreateView");
        return v;
    }

    private Button button;
    private TextView textView;

    @Override
    public void onViewCreated(@NonNull @NotNull View view, @Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mViewModel = new ViewModelProvider(this).get(MainViewModel.class);

        lifeCycleListener = new LifeCycleListener(getContext());
        getLifecycle().addObserver(lifeCycleListener);

        // 设置点击事件
        button.setOnClickListener(this);

        Log.e("===MyLocationListener", "onViewCreated");
    }

    @Override
    public void onViewStateRestored(@Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
        super.onViewStateRestored(savedInstanceState);
        Log.e("===MyLocationListener", "onViewStateRestored");
    }

    private class LifeCycleListener implements LifecycleObserver {
        private Context context;

        public LifeCycleListener(Context context) {
            this.context = context;
        }

        @OnLifecycleEvent(Lifecycle.Event.ON_START)
        void onStart() {
            showLog("onStart");
        }

        @OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
        void onResume() {
            showLog("onResume");
        }

        @OnLifecycleEvent(Lifecycle.Event.ON_PAUSE)
        void onPause() {
            showLog("onPause");
        }

        @OnLifecycleEvent(Lifecycle.Event.ON_STOP)
        void onStop() {
            showLog("onStop");
        }


        @OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
        void onDestroy() {
            showLog("onDestroy");
        }


        private void showLog(String msg) {
            Log.e("===MyLocationListener", msg);
        }
    }


    private HandlerThread myHandlerThread;
    private Handler handler;

    Thread t = new Thread(new Runnable() {
        @Override
        public void run() {
            //在子线程给handler发送数据
//                handler.sendEmptyMessage(2);
            OkHttpClient okHttpClient = new OkHttpClient();

            Request request = new Request.Builder().url("https://api.apiopen.top/recommendPoetry").build();
            try (Response response = okHttpClient.newCall(request).execute()) {
                ResponseBody body = response.body();
                if (response.isSuccessful()) {
                    Log.e("success:{}", body == null ? "" : body.string());
                } else {
                    System.out.printf("error,statusCode={},body={}", response.code(), body == null ? "" : body.string());
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    });

    private final View.OnClickListener btn_onclick = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Log.e(Tag, "btn onclicked");

            t.start();

            mViewModel.setTxt_str("hello s");
        }
    };

    @SuppressLint("NonConstantResourceId")
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.button:
                btn_onclick.onClick(button);
                break;
        }
    }
}


// this super class ??? not  keywords

